﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Fayshine.VA.RIAService.Web;

namespace Fayshine.VA.Views.SysConfig
{
    public partial class DeviceWindow : ChildWindow
    {

        private vaserver m_selectedDevice;

        public vaserver SelectedDevice
        {
            get
            {
                return m_selectedDevice;
            }
            set
            {
                m_selectedDevice = value;
            }
        }

        public List<camera> DeleteCameras
        {
            get;
            set;
        }

        public DeviceWindow(vaserver selectedDevice)
        {
            InitializeComponent();
            this.SelectedDevice = selectedDevice;
            this.LayoutRoot.DataContext = this.SelectedDevice;
            this.Title = "编辑设备 - " + selectedDevice.HostName;
            this.pwdPassword.Password = selectedDevice.Password;
            this.DeleteCameras = new List<camera>();
        }

        public DeviceWindow()
        {
            InitializeComponent();
            this.SelectedDevice = new vaserver();
            this.SelectedDevice.DeviceType = 0;
            //this.SelectedDevice.Status = 1;
            this.LayoutRoot.DataContext = this.SelectedDevice;
            this.Title = "添加新设备";
            this.DeleteCameras = new List<camera>();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            bool noError = true;

            if(string.IsNullOrEmpty(this.SelectedDevice.HostName))
            {
                noError = false;
                MessageBox.Show("主机名不能为空！");
            }
            else if (string.IsNullOrEmpty(this.SelectedDevice.CustomID.ToString()))
            {
                noError = false;
                MessageBox.Show("客户编号不能为空！");
            }
            else if (this.SelectedDevice.DeviceType <0)
            {
                noError = false;
                MessageBox.Show("设备类型不能为空！");
            }
            else  if (string.IsNullOrEmpty(SelectedDevice.Password) ||
                  !SelectedDevice.Password.Equals(this.pwdPassword.Password))
            {
                noError = false;
                MessageBox.Show("密码不一致！");
            }
            

            if (noError)
            {
                if (this.SelectedDevice.cameras.Count > 0)
                {
                    foreach (var a in this.SelectedDevice.cameras)
                    {
                        if (a.ChannelID <=0 || string.IsNullOrEmpty(a.Name))
                        {
                            noError = false;
                            MessageBox.Show("摄像头信息不完整:名称为空或者通道号不大于0");
                            break;
                        }
                    }
                }
            }

            if (noError)
            {
                this.DialogResult = true;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void btnAddCamera_Click(object sender, RoutedEventArgs e)
        {
            this.SelectedDevice.cameras.Add(new camera() { IsOnle = true, VAServerID = this.SelectedDevice.ID });
        }

        private void btnDeleteCamera_Click(object sender, RoutedEventArgs e)
        {
            var selectedCameras = this.dataGrid.GetSelectedItems<camera>();
            if (selectedCameras.Count <= 0)
            {
                MessageBox.Show("请选择要删除的摄像头");
            }
            else
            {
                if (MessageBox.Show("确定要删除选择的摄像头吗?", "确定", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    selectedCameras.ForEach(c => { 
                        this.SelectedDevice.cameras.Remove(c);
                        this.DeleteCameras.Add(c);
                    });
                }
            }
        }
    }
}

